package com.example.onlinemusicserver2024.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.mapper.ConsumerMapper;
import com.example.onlinemusicserver2024.model.domain.Consumer;
import com.example.onlinemusicserver2024.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 接口实现类
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer>
        implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    /**
     * 用于完成查询所有用户的信息
     *
     * @return
     */
    @Override
    public R allUser(IPage page) {
        // 查询得到所有的用户数据

        return R.success("查询成功", consumerMapper.selectPage(page, null));
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @Override
    public R deleteUser(int id) {
        // 第一种删除的方式，通过主键id删除
        int num = consumerMapper.deleteById(id);


        // 第二种删除的方式，通过设置where条件
//        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id",id);
//        consumerMapper.delete(queryWrapper);
        if (num > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteByIds(String[] ids) {
        // 转换参数的格式，将数组格式的参数转换为字符串
        StringBuilder sb = new StringBuilder();
        for (String id : ids) {
            sb.append(id + ",");
        }
        // 删除最后的逗号
        String newIds = sb.toString().substring(0, sb.toString().length());

        // todo 调用Mapper的删除功能
        int count = consumerMapper.deleteByIds(newIds);
        if (count > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R userDetail(int id) {
        // 设置查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        List<Consumer> consumers = consumerMapper.selectList(queryWrapper);
        return R.success("查询成功", consumers);
    }

    @Override
    public R getAllUsers() {
        List<Consumer> consumers = consumerMapper.selectList(null);
        return R.success("查询成功", consumers);
    }

    @Override
    public R addUser(Consumer consumer) {
        // TODO 2024-09-14
        // 严谨的操作：检验用户名、手机、邮箱是否可用
        String username = consumer.getUsername();
        String phoneNum = consumer.getPhoneNum();
        String email = consumer.getEmail();
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).or().eq("phone_num", phoneNum).or().eq("email", email);
        List<Consumer> consumers = consumerMapper.selectList(queryWrapper);
        if (consumers.size() > 0) {
            // 表示用户名或手机或邮箱已被占用
            return R.error("用户名或手机或邮箱已被占用,请重新注册");
        } else {
            // 设置创建时间和更新时间,头像
            consumer.setCreateTime(new Date());
            consumer.setUpdateTime(new Date());
            consumer.setAvator("https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/avatorImages/user.jpg");
            int count = consumerMapper.insert(consumer);
            if (count > 0) {
                return R.success("注册成功");
            } else {
                return R.error("注册失败");
            }
        }
    }

    @Override
    public R loginStatus(Consumer consumer, HttpSession session) {
        String username = consumer.getUsername();
        String password = consumer.getPassword();
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        long count = consumerMapper.selectCount(queryWrapper);
        if(count > 0){
            session.setAttribute("username",username);
            return R.success("登录成功",consumerMapper.selectList(queryWrapper));
        }else{
            return R.error("登录失败");
        }
    }
}
